package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.config.RedisStockConfig;
import org.example.domain.Prod;
import org.example.domain.SeckillActivity;
import org.example.domain.SeckillOrder;
import org.example.model.Result;
import org.example.model.SeckillResult;
import org.example.service.SeckillService;
import org.example.utils.AuthUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "秒杀接口管理")
@RequestMapping("seckill")
@RestController
public class SeckillController {

    @Autowired
    private SeckillService seckillService;

    @ApiOperation("获取正在进行的秒杀活动列表")
    @GetMapping("/activities")
    public Result<List<SeckillActivity>> getActiveSeckillActivities() {
        return seckillService.getActiveSeckillActivities();
    }

    @ApiOperation("获取秒杀活动详情")
    @GetMapping("/{activityId}")
    public Result<SeckillActivity> getSeckillActivity(@PathVariable Long activityId) {
        return seckillService.getSeckillActivityById(activityId);
    }

    @ApiOperation("执行秒杀")
    @PostMapping("/{activityId}/execute")
    public ResponseEntity<SeckillResult> executeSeckill(@PathVariable Long activityId) {
        //获取用户的openId
        String openId = AuthUtils.getMemberOpenId();
        SeckillResult result = seckillService.executeSeckill(activityId, openId);
        return ResponseEntity.ok(result);
    }

}
